﻿/// <reference path="../scripts/jquery-1.11.2.min.js" />
/// <reference path="../scripts/bootstrap.min.js" />

if (typeof ($) != "undefined" && $) {
    //验证用户
    CheckUser();
    $(document).ready(onReady);

}

function onReady() {
    var ua = navigator.userAgent.toLowerCase();
    var isIos = (ua.indexOf('iphone') != -1) || (ua.indexOf('ipad') != -1);

    $.ajax({
        type: "get",
        url: "api/member.ashx?ac=info&k=" + Math.random(),
        beforeSend: function () {
            $('#loadingToast').show();
        },
        error: function () {
            showToast("获取用户信息失败");
        },
        success: function (data) {
            if (data != null && data != '') {
                var m = eval("(" + data + ")");
                $("#mobile").html(m.Mobile);
                $("#hidmobile").val(m.Mobile);
                $("#truename").val(m.TrueName);
                $("#sex").html(m.Sex);
                $("#age").html(m.Age);
            }
        },
        complete: function () {
            $('#loadingToast').hide();
        }
    });

    $("#divsex").click(function () {
        var container = $('#sex_container');
        var weuiActionsheet = $('#sex_actionsheet');
        var mask = $('#sex_mask');

        container.height("100%");
        weuiActionsheet.addClass('weui_actionsheet_toggle');
        mask.show().addClass('weui_fade_toggle').click(function () {
            hideActionSheet(container, weuiActionsheet, mask);
        });
        $('#sex_cancel').click(function () {
            hideActionSheet(container, weuiActionsheet, mask);
        });
        weuiActionsheet.unbind('transitionend').unbind('webkitTransitionEnd');
    });

    $("#divage").click(function () {
        var container = $('#age_container');
        var weuiActionsheet = $('#age_actionsheet');
        var mask = $('#age_mask');

        container.height("100%");
        weuiActionsheet.addClass('weui_actionsheet_toggle');
        mask.show().addClass('weui_fade_toggle').click(function () {
            hideActionSheet(container, weuiActionsheet, mask);
        });
        $('#age_cancel').click(function () {
            hideActionSheet(container, weuiActionsheet, mask);
        });
        weuiActionsheet.unbind('transitionend').unbind('webkitTransitionEnd');
    });

    //保存
    $("#btnSave").click(function () {
        var truename = $("#truename").val();
        var sex = $("#sex").html();
        var age = $("#age").html()

        if (truename == '') {
            showToast('请填写称呼');
        }
        else if (sex == '') {
            showToast('请填写称谓');
        }
        else if (age == '') {
            showToast('请填写年龄');
        }
        else {
            var data = { truename: truename, sex: sex, age: age };
            $.ajax({
                type: "post",
                data: data,
                url: "api/member.ashx?ac=upInfo&k=" + Math.random(),
                beforeSend: function () {
                    $('#loadingToast').show();
                },
                error: function () {
                    showToast("数据提交失败");
                },
                success: function (data) {
                    var obj = eval("(" + data + ")").result;
                    if (obj.r == "suc") {
                        showToast('保存成功');
                        window.location.href = "info.html";
                    }
                    else {
                        showToast(obj.val);
                    }
                },
                complete: function () {
                    $('#loadingToast').hide();
                }
            });
        }
    });
}
function setSex(val) {
    $("#sex").html(val);
    var container = $('#sex_container');
    var weuiActionsheet = $('#sex_actionsheet');
    var mask = $('#sex_mask');
    hideActionSheet(container, weuiActionsheet, mask);
}
function setAge(val) {
    $("#age").html(val);
    var container = $('#age_container');
    var weuiActionsheet = $('#age_actionsheet');
    var mask = $('#age_mask');
    hideActionSheet(container, weuiActionsheet, mask);
}
function hideActionSheet(container, weuiActionsheet, mask) {
    container.height("0");
    weuiActionsheet.removeClass('weui_actionsheet_toggle');
    mask.removeClass('weui_fade_toggle');
    weuiActionsheet.on('transitionend', function () {
        mask.hide();
    }).on('webkitTransitionEnd', function () {
        mask.hide();
    })
}


